//
//  UIView+ML_Public.m
//  hehe
//
//  Created by Robin on 15/12/14.
//  Copyright © 2015年 HolyShit. All rights reserved.
//

#import "UIView+MY_Public.h"

@implementation UIView (MY_Public)

-(void)makeRoundWithCorner:(UIRectCorner)corner radius:(CGFloat)radius
{
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
}

-(void)makeRound:(BOOL)roundOrNot
{
    self.layer.shouldRasterize = YES;
    self.layer.rasterizationScale = [UIScreen mainScreen].scale;
#ifdef __IPHONE_8_1
    if(roundOrNot){
        self.clipsToBounds = YES;
        //self.layer.masksToBounds = YES;
        self.layer.cornerRadius = ceil(self.height) / 2;
    }
    else{
        self.clipsToBounds = NO;
        self.layer.cornerRadius = 0;
    }
#else
    CAShapeLayer *maskLayer = (id)self.layer.mask;
    if(roundOrNot){
        if(maskLayer==nil){
            CAShapeLayer *maskLayer = [CAShapeLayer layer];
//            maskLayer.fillColor = [UIColor blackColor].CGColor;
            
            CGMutablePathRef pathRef = CGPathCreateMutable();
            CGPathAddEllipseInRect(pathRef, NULL, self.bounds);
            maskLayer.path = pathRef;
            CGPathRelease(pathRef);
            
            maskLayer.frame = self.bounds;
            self.layer.mask = maskLayer;
        }
    }
    else{
        [maskLayer removeFromSuperlayer];
    }
#endif
}

-(UIImage*)convertToImage{
    UIGraphicsBeginImageContext(self.bounds.size);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

-(void)addAlphaBlurWithBgColor:(UIColor *)bgColor alpha:(CGFloat)alpha
{
    CGRect bounds = self.bounds;
    UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]];
    visualEffectView.frame = bounds;
    visualEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self addSubview:visualEffectView];
    self.backgroundColor = [bgColor colorWithAlphaComponent:alpha];
    [self sendSubviewToBack:visualEffectView];
}

- (void)waterRipple
{
    [self waterRippleWithDuration:1];
}

- (void)waterRippleWithDuration:(CGFloat)duration
{
    if(!self){
        return;
    }
    CATransition *trasition = [CATransition animation];
    trasition.duration = duration;
    [trasition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    trasition.type = @"rippleEffect";
    [self.layer addAnimation:trasition forKey:@"animation"];
}

@end

@implementation UIWindow (MY_Window)
+(UIWindow *)my_topWindow
{
    UIWindow *window = nil;
    if ([MYAppHelper shareHelper].isKeyboardVisible) {
        window = [[[UIApplication sharedApplication] windows] lastObject];
    }
    if (!window){
        window = [[UIApplication sharedApplication] keyWindow];
    }
    return window;
}

@end


@implementation UIView (MY_LoadXib)
+ (id)my_loadFromXIB
{
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil] lastObject];
}

+ (id)my_loadFromXIBName:(NSString *)xibName
{
    return [[[NSBundle mainBundle] loadNibNamed:xibName owner:nil options:nil] lastObject];
}
@end

@implementation UITableViewCell (MY_SelectedBg)

-(void)my_setSelectedBg
{
    self.backgroundColor = self.contentView.backgroundColor = [UIColor whiteColor];
    
    UIView *selectedBgView = [UIView new];
    selectedBgView.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    
    self.selectionStyle = UITableViewCellSelectionStyleBlue;
    self.selectedBackgroundView = selectedBgView;
}
-(void)my_ClearSelectedBg
{
    self.backgroundColor = self.contentView.backgroundColor = [UIColor whiteColor];
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.selectedBackgroundView = nil;
}

@end
